package com.gopay.scheduler.job;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.DrawOpType;
import com.gopay.common.constants.trans.PayBankOpType;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.constants.wallet.optype.Wallet01605OpType;
import com.gopay.common.constants.wallet.optype.Wallet01606OpType;
import com.gopay.common.constants.wallet.optype.Wallet01617OpType;
import com.gopay.common.domain.cps.gp.GP01301VO;
import com.gopay.common.domain.cps.transfer.Tran00700VO;
import com.gopay.common.domain.cps.transfer.Tran00701PortalVO;
import com.gopay.common.domain.cps.transfer.Tran00701VO;
import com.gopay.common.domain.cps.transfer.Tran00800VO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.domain.cps.wallet.Wallet01605VO;
import com.gopay.common.domain.cps.wallet.Wallet01606VO;
import com.gopay.common.domain.cps.wallet.Wallet01617VO;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;

/**
 * 自动结算处理银企直连成功后的乐观锁异常Job
 * @AutoSettleOptimiLockJob.java
 * @author fanghw
 * @2015年4月3日 上午9:27:34  www.gopay.com.cn Inc.All rights reserved.
 */
public class AutoSettleOptimiLockJob extends GopayBaseJob {
    // 日志
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;
    
    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;
    
    @Override
    public void doJobInternal() {
        
        
        //自动结算之重做乐观锁异常订单
        doAutoSettleRedoOptimiLockOrder(); 
        
        
        
        
        
        logger.error("AutoSettleManualLaunchOrderJob------start------");
        //自动结算之手动发起订单
        doAutoSettleManualLaunchOrder();
        
        logger.error("AutoSettleManualLaunchOrderJob------end------");
    }
    
    /**
     * 自动结算之重做乐观锁异常订单
     */
    private void doAutoSettleRedoOptimiLockOrder(){
        StringBuilder sb = new StringBuilder();
        
        sb.append(" select m.gopay_order_id,m.gopay_int_txn_cd from cps_gen_main_order m  ");
        sb.append(" left join cps_gen_pay_order p on p.gopay_order_id=m.gopay_order_id ");
        sb.append("  where 1=1 ");
        sb.append("  and p.auto_settle='1' ");
        sb.append("  and m.accounting_point='1' ");
        sb.append("  and m.txn_sta_cd="+TxnStaCode.TXN_STA_CD_30003.value);
        sb.append("  and m.gopay_int_txn_cd in ('00700','00800','01301','01605','01606') ");
        sb.append("  and p.cust_audit_time>= to_timestamp(:preMonth, 'yyyymmdd') ");
        
               
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -10);
        //日期T-30
        String preMonth=DateUtils.format(calendar.getTime(), "yyyyMMdd");
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("preMonth", preMonth);

        List<Object[]> rsList = commonQueryDAO.findBySql(sb.toString(), map);
        String gopayOrderId,intTxnCd;
        Object paramVo;
        CPSResponse response;
        if (rsList != null && rsList.size() > 0) {
            logger.error("OptimisticLockOrder--rsList.size="+rsList.size());
            
            for (Object[] arr : rsList) {
                gopayOrderId=(String) arr[0];
                intTxnCd=(String) arr[1];
                
                // 封装cps请求参数vo
                paramVo = buildCallCpsParamOfOptimiLock(gopayOrderId, intTxnCd);
                
                try {
                    logger.error("callCps-------gopayOrderId="+gopayOrderId+",intTxnCd="+intTxnCd);
                    // 调用cps
                    response=callCPSHelper.callByVOTicket(paramVo);
                    if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                        logger.error("------调用cps返回异常  response="+response.getRespCode());
                    }

                } catch (Exception e) {
                    // 吃掉所有异常
                    logger.error("callByVOTicket Error", e);
                }
            }
        }
    }
    
    /**
     * 封装cps请求参数vo
     * 
     * @param gopayOrderId
     * @param intTxnCd
     * @return
     */
    private Object buildCallCpsParamOfOptimiLock(String gopayOrderId,String intTxnCd) {
        Object obj = null;
        if (intTxnCd.equals(IntTxnCd._00700.value)) {
            Tran00700VO paramVo = new Tran00700VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setOpType(PayBankOpType.AUTO_SETTLE_REDO_OPTIMI_LOCK_ORDER);

            obj = paramVo;
        } else if (intTxnCd.equals(IntTxnCd._00800.value)) {
            Tran00800VO paramVo = new Tran00800VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setOpType(DrawOpType.AUTO_SETTLE_REDO_OPTIMI_LOCK_ORDER);

            obj = paramVo;
        } else if (intTxnCd.equals(IntTxnCd._01301.value)) {
            GP01301VO paramVo = new GP01301VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setStepType("11");

            obj = paramVo;
        } else if (intTxnCd.equals(IntTxnCd._01605.value)) {
        	Wallet01605VO paramVo = new Wallet01605VO();
        	paramVo.setGopayOrderId(gopayOrderId);
        	paramVo.setOpType(Wallet01605OpType.AUTO_SETTLE_REDO_OPTIMI_LOCK_ORDER);
        	
        	obj = paramVo;
        } else if (intTxnCd.equals(IntTxnCd._01606.value)) {
        	Wallet01606VO paramVo = new Wallet01606VO();
        	paramVo.setGopayOrderId(gopayOrderId);
        	paramVo.setOpType(Wallet01606OpType.AUTO_SETTLE_REDO_OPTIMI_LOCK_ORDER);
        	
        	obj = paramVo;
        }else if (intTxnCd.equals(IntTxnCd._00701.value)) {
            Tran00701VO paramVo = new Tran00701VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setOpType(PayBankOpType.AUTO_SETTLE_REDO_OPTIMI_LOCK_ORDER);
            Tran00701PortalVO tran00701PortalVO = new Tran00701PortalVO();
            tran00701PortalVO.setTran00701VO(paramVo);
            obj = tran00701PortalVO;
        } else if (intTxnCd.equals(IntTxnCd._01617.value)) {//天溢盈兑付到银行 add by zhoupeng 20170308
        	Wallet01617VO paramVo = new Wallet01617VO();
        	paramVo.setGopayOrderId(gopayOrderId);
        	paramVo.setOpType(Wallet01617OpType.AUTO_SETTLE_REDO_OPTIMI_LOCK_ORDER);
        	
        	obj = paramVo;
        }

        return obj;
    }
    
    /**
     * 自动结算之手动发起订单
     */
    private void doAutoSettleManualLaunchOrder(){
        StringBuilder sb = new StringBuilder();
        
        sb.append(" select m.gopay_order_id,m.gopay_int_txn_cd from cps_gen_main_order m  ");
        sb.append(" left join cps_gen_pay_order p on p.gopay_order_id=m.gopay_order_id ");
        sb.append("  where 1=1 ");
        sb.append("  and p.PAY_STAT='06' ");
        sb.append("  and p.auto_settle='1' ");
        sb.append("  and m.accounting_point='1' ");
        sb.append("  and m.txn_sta_cd="+TxnStaCode.TXN_STA_CD_30000.value);
        sb.append("  and m.gopay_int_txn_cd in ('00700','00800','01301','00701') ");
        sb.append("  and p.cust_audit_time>= to_timestamp(:preMonth, 'yyyymmdd') ");
        sb.append(" union ");
        sb.append(" select m.gopay_order_id,m.gopay_int_txn_cd from cps_gen_main_order m  ");
        sb.append(" left join cps_gen_pay_order p on p.gopay_order_id=m.gopay_order_id ");
        sb.append("  where 1=1 ");
        sb.append("  and p.PAY_STAT='06' ");
        sb.append("  and p.auto_settle='1' ");
        sb.append("  and m.accounting_point='1' ");
        sb.append("  and m.txn_sta_cd="+TxnStaCode.TXN_STA_CD_30402.value);
        sb.append("  and m.gopay_int_txn_cd in ('01605','01606') ");
        sb.append("  and p.cust_audit_time>= to_timestamp(:preMonth, 'yyyymmdd') ");
        
               
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -10);
        //日期T-30
        String preMonth=DateUtils.format(calendar.getTime(), "yyyyMMdd");
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("preMonth", preMonth);

        List<Object[]> rsList = commonQueryDAO.findBySql(sb.toString(), map);
        String gopayOrderId,intTxnCd;
        Object paramVo;
        CPSResponse response;
        if (rsList != null && rsList.size() > 0) {
            logger.error("doAutoSettleManualLaunchOrder--rsList.size="+rsList.size());
            
            for (Object[] arr : rsList) {
                gopayOrderId=(String) arr[0];
                intTxnCd=(String) arr[1];
                
                // 封装cps请求参数vo
                paramVo = buildCallCpsParamOfManualLaunch(gopayOrderId, intTxnCd);
                
                try {
                    logger.error("doAutoSettleManualLaunchOrder callCps-------gopayOrderId="+gopayOrderId+",intTxnCd="+intTxnCd);
                    // 调用cps
                    response=callCPSHelper.callByVOTicket(paramVo);
                    if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                        logger.error("------doAutoSettleManualLaunchOrder 调用cps返回异常  response="+response.getRespCode());
                    }

                } catch (Exception e) {
                    // 吃掉所有异常
                    logger.error("doAutoSettleManualLaunchOrder callByVOTicket Error", e);
                }
            }
        }
    }
    
    private Object buildCallCpsParamOfManualLaunch(String gopayOrderId,String intTxnCd) {
        Object obj = null;
        if (intTxnCd.equals(IntTxnCd._00700.value)) {
            Tran00700VO paramVo = new Tran00700VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setOpType(PayBankOpType.AUTO_SETTLE_MANUAL_BCD_LOCK_DEAL);

            obj = paramVo;
        } else if (intTxnCd.equals(IntTxnCd._00800.value)) {
            Tran00800VO paramVo = new Tran00800VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setOpType(DrawOpType.AUTO_SETTLE_MANUAL_BCD_LOCK_DEAL);

            obj = paramVo;
        } else if (intTxnCd.equals(IntTxnCd._01301.value)) {
            GP01301VO paramVo = new GP01301VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setStepType("12");//自动结算之手动银企直连锁定处理

            obj = paramVo;
        } else if (intTxnCd.equals(IntTxnCd._01605.value)) {
        	Wallet01605VO paramVo = new Wallet01605VO();
        	paramVo.setGopayOrderId(gopayOrderId);
        	paramVo.setOpType(Wallet01605OpType.AUTO_SETTLE_MANUAL_BCD_LOCK_DEAL);
        	
        	obj = paramVo;
        } else if (intTxnCd.equals(IntTxnCd._01606.value)) {
        	Wallet01606VO paramVo = new Wallet01606VO();
        	paramVo.setGopayOrderId(gopayOrderId);
        	paramVo.setOpType(Wallet01606OpType.AUTO_SETTLE_MANUAL_BCD_LOCK_DEAL);
        	
        	obj = paramVo;
        }else if (intTxnCd.equals(IntTxnCd._00701.value)) {
            Tran00701VO paramVo = new Tran00701VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setOpType(PayBankOpType.AUTO_SETTLE_MANUAL_BCD_LOCK_DEAL);
            Tran00701PortalVO tran00701PortalVO = new Tran00701PortalVO();
            tran00701PortalVO.setTran00701VO(paramVo);
            obj = tran00701PortalVO;
        } else if (intTxnCd.equals(IntTxnCd._01617.value)) {//天溢盈兑付到银行 add by zhoupeng 20170308
            Wallet01617VO paramVo = new Wallet01617VO();
            paramVo.setGopayOrderId(gopayOrderId);
            paramVo.setOpType(Wallet01617OpType.AUTO_SETTLE_MANUAL_BCD_LOCK_DEAL);
            
            obj = paramVo;
        }
        return obj;
    }

}
